package com.migo.controller;

import com.migo.entity.OpeningAdvertisementEntity;
import com.migo.entity.SysUserEntity;
import com.migo.service.ImageService;
import com.migo.service.OpeningAdvertisementService;
import com.migo.service.SysUserService;
import com.migo.utils.DateUtils;
import com.migo.utils.PageUtils;
import com.migo.utils.Query;
import com.migo.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.migo.utils.ShiroUtils.getUserId;


/**
 * @author powervision
 * @email wes.wang@powervision.me
 * @date 2018-10-29 14:29:47
 */
@RestController
@RequestMapping("openingadvertisement")
public class OpeningAdvertisementController {

    @Autowired
    private OpeningAdvertisementService openingAdvertisementService;

    @Autowired
    private ImageService imageService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("openingadvertisement:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<OpeningAdvertisementEntity> openingAdvertisementList = openingAdvertisementService.queryList(query);
        int total = openingAdvertisementService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(openingAdvertisementList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("openingadvertisement:info")
    public R info(@PathVariable("id") Long id) {
        OpeningAdvertisementEntity openingAdvertisement = openingAdvertisementService.queryObject(id);

        return R.ok().put("openingAdvertisement", openingAdvertisement);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("openingadvertisement:save")
    public R save(@RequestBody OpeningAdvertisementEntity openingAdvertisement) {
        if (StringUtils.isEmpty(openingAdvertisement.getOpenImage())) {
            String url = imageService.uploadOne(openingAdvertisement.getOpenImage());
            SysUserEntity user = sysUserService.queryObject(getUserId());
            openingAdvertisement.setAddTime(DateUtils.getNow());
            openingAdvertisement.setAddUser(user.getUsername());
            openingAdvertisement.setOpenImage(url);
            openingAdvertisementService.save(openingAdvertisement);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("openingadvertisement:update")
    public R update(@RequestBody OpeningAdvertisementEntity openingAdvertisement) {
        if (StringUtils.isEmpty(openingAdvertisement.getOpenImage())) {
            String url = imageService.uploadOne(openingAdvertisement.getOpenImage());
            openingAdvertisement.setOpenImage(url);
        }
        openingAdvertisementService.update(openingAdvertisement);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("openingadvertisement:delete")
    public R delete(@RequestBody Long[] ids) {
        openingAdvertisementService.deleteBatch(ids);

        return R.ok();
    }

}
